package com.justenholter.text.effects {
	
	import com.justenholter.text.effects.IEffect;
	
	/**
	 * Inserts new text before the current text one character
	 * at a time.
	 * 
	 * @author justen holter
	 */
	public class Insert implements IEffect {
		
		private var _curText:String;
		private var _newText:String;
		private var _offset:Number;
		
		/**
		 * Creates a new instance of Insert.
		 */
		function Insert() {
			
		}  
		
		/**
		 * @inheritDoc
		 */
		public function init(curText:String, newText:String):String {
			_curText = curText;
			_newText = newText;
			_offset = 1;
			return next(curText, newText);
		}
		
		/**
		 * @inheritDoc
		 */
		public function next(curText:String, newText:String):String {
			
			if (_curText == null || _newText == null) init(curText, newText);
			
			var a:String = _newText.slice(0, _offset);
			var b:String = _curText.slice(0, _curText.length - Math.min(_curText.length, _offset));
			_offset++;
			
			return a + b;
		}
	}
}